package com.metadata_system.api.handler;

import com.metadata_system.api.exceptions.LimitException;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.exception.ApiException;
import com.metadata_system.common.exception.ApiSignException;
import com.metadata_system.common.exception.FormFieldErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // 添加这个异常处理方法
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseEntity<ApiResult> handler(MissingServletRequestParameterException e) {
        String parameterName = e.getParameterName();
        String errorMessage = String.format("缺少必要参数: %s", parameterName);

        ApiResult backResult = ApiResult.builder()
                .code(105)  // 可以自定义错误码
                .message(errorMessage)
                .build();

        log.warn("缺少请求参数: {}", parameterName);
        return new ResponseEntity<>(backResult, HttpStatus.BAD_REQUEST);
    }

    // Api通用错误
    @ExceptionHandler(ApiException.class)
    public ResponseEntity<ApiResult> handler(ApiException e) {
        ApiResult backResult = ApiResult.builder().code(1).message(e.getMessage()).build();
        return new ResponseEntity<ApiResult>(backResult, HttpStatus.OK);
    }


    // 处理身份异常
    @ExceptionHandler(SecurityException.class)
    public ResponseEntity<ApiResult> handler(SecurityException e) {
        e.printStackTrace();
        ApiResult backResult = ApiResult.builder()
                .code(101)
                .message("登录令牌错误")
                .build();
        return new ResponseEntity<ApiResult>(backResult, HttpStatus.OK);
    }

    // 处理表单字段异常
    @ExceptionHandler(FormFieldErrorException.class)
    public ResponseEntity<ApiResult> handler(FormFieldErrorException e) {
        ApiResult backResult = ApiResult.builder()
                .code(102)
                .message(e.getMessage())
                .build();
        return new ResponseEntity<ApiResult>(backResult, HttpStatus.OK);
    }


    // 处理访问频率过高异常
    @ExceptionHandler(LimitException.class)
    public ResponseEntity<ApiResult> handler(LimitException e) {
        ApiResult backResult = ApiResult.builder()
                .code(103)
                .message(e.getMessage())
                .build();
        return new ResponseEntity<ApiResult>(backResult, HttpStatus.OK);
    }

    // 处理访问频率过高异常
    @ExceptionHandler(ApiSignException.class)
    public ResponseEntity<ApiResult> handler(ApiSignException e) {
        System.out.println("e.getMessage()"+e.getMessage());
        ApiResult backResult = ApiResult.builder()
                .code(104)
                .message(e.getMessage())
                .build();
        return new ResponseEntity<ApiResult>(backResult, HttpStatus.OK);
    }
}
